//
//  AnimationStore.mm
//  Cocos2dAction
//
//  Created by waver on 10-7-8.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AnimationStore.h"


@implementation CAnimationStoreW

static CAnimationStoreW* __self = nil;

+(id)alloc
{
	return [super alloc];
}

-(id)Init
{
	__PlayStore = [[NSMutableDictionary alloc] init];
	__EditStore = [[NSMutableDictionary alloc] init];
	
	__NameStore = [[NSMutableDictionary alloc] init];
	
	return self;
}

+(CAnimationStoreW*)ShareAnimationStore
{
	if(__self == nil)
	{
		__self = [[CAnimationStoreW alloc] Init];
	}
	return __self;
}

-(BOOL)AddEditorFile:(NSString*)fileName EditorKind:(int)kind Tag:(int)intTag
{
	NSString * fileNameStr = [NSString stringWithString:fileName];
	//[fileNameStr retain];
	NSNumber* tgNum = [NSNumber numberWithInt:intTag];
	if(kind == SINGLELEMENT)
	{
		
		CCEditorAction * findAction = [__EditStore objectForKey:fileNameStr];
		
		if (nil == findAction) {
			findAction = [CCEditorAction alloc];
			[findAction InitAction:fileNameStr];
			if(nil != findAction)
			{
				[__EditStore setObject:findAction forKey:fileNameStr];
				[__NameStore setObject:fileNameStr forKey:tgNum];
				return true;
			}
			else
			{
				return false;
			}

		}
		return true;
	}
	else
	{
		CAnimationPlayW * findPlay = [__PlayStore objectForKey:fileNameStr];
		
		if(nil == findPlay)
		{
			findPlay = [CAnimationPlayW alloc];
			[findPlay InitAction:fileNameStr];
			if(nil != findPlay)
			{
				[__PlayStore setObject:findPlay forKey:fileNameStr];
				[__NameStore setObject:fileNameStr forKey:tgNum];
				return true;
			}
			else
			{
				return false;
			}
		}
		return true;
	}

}

-(CCAnimation*)CreateAnimation:(int)aniIndex doubleTime:(int)times fileTag:(int)intTag 
{
	CCEditorAction * findEditor = [self FindObjectFormTag:intTag];
	return [findEditor AnimationInEditor:aniIndex doubleTime:times];
}

-(CActionSpriteW*)CreateActionSprite: (int)aniIndex doubleTime:(int)times  cyc:(BOOL)IsCys fileTag:(int)intTag 
{
	CAnimationPlayW * findPlay = [self FindObjectFormTag:intTag];
	return [findPlay CreateActionSprite: aniIndex doubleTime:times  cyc:IsCys];
}

-(id)FindObjectFormTag:(int)intTag
{
	NSNumber* tgNum = [NSNumber numberWithInt:intTag];
	NSString * fileName = [__NameStore objectForKey:tgNum];
	id find = [__PlayStore objectForKey:fileName];
	if(find == nil)
	{
		return [__EditStore objectForKey:fileName];
	}
	return find;
}


@end
